package com.xiongtian.tree;

/**
 * @author xiongtian
 * @version 1.0
 * @date 2021/4/15 19:18
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        // 先需要创建一棵二叉数
        BinaryTree binaryTree = new BinaryTree();
        // 创建啊需要的节点

        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");

        binaryTree.setRoot(root);
        // 说明，我们先手动创建该二叉树，后面学习递归的方式创建二叉树
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);


        // 测试
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("中序遍历");
        binaryTree.infixOrder();
        System.out.println("后序遍历");
        binaryTree.postOrder();
        // 查找
        System.out.println("查找");
        HeroNode heroNode = binaryTree.postOrderSearch(root, 2);
        System.out.println(heroNode.toString());

        // 删除
        System.out.println("删除节点！");
        binaryTree.delNode(2);
        binaryTree.preOrder();
    }
}

// 先创建节点
class HeroNode {
    private int no;
    private String name;
    private HeroNode left; // 默认null
    private HeroNode right; // 默认null

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    // 编写前序遍历的方法
    public void preOrder(HeroNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.toString());
        preOrder(node.left);
        preOrder(node.right);

    }

    // 编写中序遍历的方法
    public void infixOrder(HeroNode node) {
        if (node == null) {
            return;
        }
        infixOrder(node.left);
        System.out.println(node.toString());
        infixOrder(node.right);
    }

    // 编写后序遍历的方法
    public void postOrder(HeroNode node) {
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.toString());
    }

    public HeroNode preOrderSearch(HeroNode node, int no) {
        if (node == null) {
            return null;
        }
        if (node.no == no) {
            return node;
        }
        HeroNode heroNode = null;

        heroNode = preOrderSearch(node.left, no);
        if (heroNode != null) {
            return heroNode;
        }
        heroNode = preOrderSearch(node.right, no);
        if (heroNode != null) {
            return heroNode;
        }
        return heroNode;
    }

    public HeroNode infixOrderSearch(HeroNode node, int no) {
        if (node == null) {
            return null;
        }
        HeroNode heroNode = null;

        heroNode = infixOrderSearch(node.left, no);
        if (heroNode != null) {
            return heroNode;
        }
        if (node.no == no)
            return node;
        heroNode = infixOrderSearch(node.right, no);
        if (heroNode != null) {
            return heroNode;
        }
        return heroNode;
    }

    public HeroNode postOrderSearch(HeroNode node, int no) {
        if (node == null) {
            return null;
        }

        HeroNode heroNode = null;

        heroNode = postOrderSearch(node.left, no);
        if (heroNode != null) {
            return heroNode;
        }
        heroNode = postOrderSearch(node.right, no);
        if (heroNode != null) {
            return heroNode;
        }
        if (node.no == no)
            return node;
        return heroNode;
    }

    /**
     * 递归删除节点的方法
     * 1. 如果删除的是叶子节点，则删除该节点
     * 2. 如果删除的是非叶子节点，则删除该子树
     */
    public void delNode(int no) {
        //if (this.left != null && this.left.no == no)
        if (left != null) {
            if (this.left.no == no) {
                this.left = null;
                return;
            }
        }
        if (right != null) {
            if (this.right.no == no) {
                this.right = null;
                return;
            }
        }
        if (left != null) {
            left.delNode(no);
        }
        if (right != null) {
            right.delNode(no);
        }
    }
}

//定义BinaryTree二叉树
class BinaryTree {

    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    // 前序遍历
    public void preOrder() {
        if (this.root != null)
            this.root.preOrder(root);
        else
            System.out.println("二叉树为空无法遍历！");
    }

    // 中序遍历
    public void infixOrder() {
        if (this.root != null)
            this.root.infixOrder(root);
        else
            System.out.println("二叉树为空无法遍历！");
    }

    // 后序遍历
    public void postOrder() {
        if (this.root != null)
            this.root.postOrder(root);
        else
            System.out.println("二叉树为空无法遍历！");
    }

    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(root, no);
        } else {
            return null;
        }
    }

    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return root.infixOrderSearch(root, no);
        } else {
            return null;
        }
    }

    public HeroNode postOrderSearch(HeroNode root, int no) {
        if (this.root != null) {
            return this.root.postOrderSearch(this.root, no);
        } else {
            return null;
        }
    }

    public void delNode(int i) {
        if (root != null) {
            // 如果只有一个root节点，这里立即判断root是不是就是要删除的节点
            if (i == root.getNo()) {
                root = null;
                return;
            }
            root.delNode(i);
        }else {
            System.out.println("空树，不能删除！");
        }


    }
}